package com.dongdongshop.config;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component//首先实例化
@Aspect //表示当前类是一个切面类
public class LogAop {

    private Logger logger = LoggerFactory.getLogger(LogAop.class);

    //切点和通知
    @Pointcut("execution(public * com.dongdongshop.service.*.*(..)) || @annotation(com.dongdongshop.log.DongKeLog)")
    public void poincut() {

    }

    //定义通知: 前置，后置，返回，异常，环绕
    @Before("poincut()")
    public void aopBefore(JoinPoint joinPoint) {
        //前置通知打印参数
        Object[] args = joinPoint.getArgs();
        logger.info("正在进入 {} 方法,参数为 {} ",joinPoint.getSignature(),args);
    }

    @AfterReturning(value = "poincut()",returning = "obj")
    public void aopAfter(Object obj) throws Throwable {
            logger.info("方法调用成功,返回的结果为: {} ", JSONObject.toJSONString(obj));
    }
}
